<?php
/**
 * CreateUser: zlh
 * CreateTime: 2022/1/18 11:32
 * Remark:该类主要实现了对钉钉内部消息的操作
 */

namespace App\Tools\DingTalk;

use App\Consts\DingTalkConsts\DingTalkConfigConsts;
use App\Consts\ResponseConsts\ResponseStatusConsts;
use App\Func\AppFunc;
use App\Func\ResponseFunc;
use Head;
use Illuminate\Http\Exceptions\HttpResponseException;
use OapiMessageCorpconversationAsyncsendV2Request;
use Msg;
use Text;
use OA;
use Body;
use Rich;
use StatusBar;

class DingTalkMsgFacade extends BaseDingTalk
{


    # send ding_talk msg by ding_talk msg type
    # msgType : text
    #          Params : text.content : "消息体"
    # msgType : oa
    #          Params : oa.head : "消息头"
    #                   oa.head.bgcolor : "消息头部的背景颜色" (FFBBBBBB)
    #                   oa.head.text : "头部标题,长度限制为最多10个字符"
    #                   oa.body : "消息体"
    #                   oa.body.title : "消息体的标题，建议50个字符以内"
    #                   oa.body.content : "消息体的内容，最多显示3行"
    #                   oa.body.image : "消息体中的图片，支持图片资源@mediaId。建议宽600像素 x 400像素，宽高比3 : 2"
    #                   oa.body.rich : "单行富文本信息"
    #                   oa.body.rich.num : "单行富文本信息的数目"
    #                   oa.body.rich.unit : "单行富文本信息的单位"
    #                   oa.body.file_count : "自定义的附件数目。此数字仅供显示，钉钉不作验。"
    #                   oa.body.author : "自定义的作者名字"
    #                   oa.message_url : "消息点击链接地址，当发送消息为小程序时支持小程序跳转链接"
    #                   oa.pc_message_url : "PC端点击消息时跳转到的地址"
    #

    /**
     * 根据用户List发送消息 用户List为 10001,10002 示例
     * @param string $userList
     * @param array $sendMsgData
     * @return bool
     */
    public static function sendOAMessageByUserList(string $userList,array $sendMsgData): bool
    {
        $req = new OapiMessageCorpconversationAsyncsendV2Request;
        $baseUrl = "https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2";
        $req->setAgentId(AppFunc::getDingTalkConfig()::DING_TALK_AGENT_ID);
        $req->setUseridList($userList);
        return self::extracted($req, $sendMsgData, $baseUrl);
    }

    /**
     * 根据部门发送消息 用户List为 10001,10002 示例
     * @param string $deptList
     * @param array $sendMsgData
     * @return bool
     */
    public static function sendOAMessageByDeptList(string $deptList,array $sendMsgData): bool
    {
        $req = new OapiMessageCorpconversationAsyncsendV2Request;
        $baseUrl = "https://oapi.dingtalk.com/topapi/message/corpconversation/asyncsend_v2";
        $req->setAgentId(AppFunc::getDingTalkConfig()::DING_TALK_AGENT_ID);
        $req->setDeptIdList($deptList);
        return self::extracted($req, $sendMsgData, $baseUrl);
    }

    /**
     * 发送消息主体方法
     * @param OapiMessageCorpconversationAsyncsendV2Request $req
     * @param array $sendMsgData
     * @param string $baseUrl
     * @return bool
     */
    public static function extracted(OapiMessageCorpconversationAsyncsendV2Request $req, array $sendMsgData, string $baseUrl): bool
    {
        $msg = new Msg;
        $msg->msgtype="oa";
        $text = new Text;
        $text->content= $sendMsgData['content'];
        $msg->text = $text;
        $oa = new OA;
        $body = new Body;
        $body->author="系统管理员";
        $body->content=$sendMsgData['content'];
//        $rich = new Rich;
//        $rich->num="日期";
//        $rich->unit=date("Y-m-d h:i:s");
//        $body->rich = $rich;
        $body->title= $sendMsgData['title'];
        $oa->body = $body;
        $head = new Head;
//        $head->bgcolor="FFBBBBBB";
//        $head->text="消息头部内容。";
        $oa->head = $head;
//        $status_bar = new StatusBar;
//        $status_bar->status_value="审批状态";
//        $status_bar->status_bg="FFBBBBBB";
//        $oa->status_bar = $status_bar;
//        $oa->pc_message_url="www.baidu.com";
//        $oa->message_url="www.baidu.com";
        $msg->oa = $oa;

        $req->setMsg($msg);
        try {
            $Res = self::getDingTalkOldClient()->execute($req, self::getAccessToken(), $baseUrl);

        } catch (\Exception $err) {
            throw new HttpResponseException(ResponseFunc::returnJson(
                'error',
                '',
                "钉钉客户端错误" . $err->getMessage(),
                ResponseStatusConsts::INTERNAL_SERVER_ERROR_500_RESPONSE_STATUS
            ));
        }
        return true;
    }
}